﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using PSE.Deployment.Engine.Enums;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Text.RegularExpressions;
using PSE.Deployment.SetupBase.UI;

namespace PSE.Deployment.SetupBase.UI
{
    public class BaseWizardWindow : BaseWindow
    {
        #region Propriedades

        public bool AllowNext
        {
            get { return (bool)GetValue(AllowNextProperty); }
            set { SetValue(AllowNextProperty, value); }
        }

        public static readonly DependencyProperty AllowNextProperty =
            DependencyProperty.Register("AllowNext", typeof(bool), typeof(BaseWizardWindow), new UIPropertyMetadata(false));


        private Button _btnNext;
        private Button _btnPrevious;

        #endregion

        #region Construtor

        public BaseWizardWindow()
        {
            this.ConfirmOnClosing = true;
            this.Loaded += new RoutedEventHandler(BaseWizardWindow_Loaded);
        }

        #endregion

        #region Eventos Window

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == System.Windows.Input.Key.Enter && this.AllowNext)
            {
                if (_btnNext != null && _btnNext.Visibility == Visibility.Visible && _btnNext.IsEnabled)
                    BtnNext_Click(_btnNext, new RoutedEventArgs(Button.ClickEvent, _btnNext));
            }
            else if (e.Key == System.Windows.Input.Key.Back)
            {
                if (_btnPrevious != null && _btnPrevious.Visibility == Visibility.Visible && _btnPrevious.IsEnabled)
                    BtnPrevious_Click(_btnPrevious, new RoutedEventArgs(Button.ClickEvent, _btnPrevious));
            }
        }

        private void BaseWizardWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _btnNext = this.FindName("BtnNext") as Button;
            if (_btnNext != null)
            {
                _btnNext.Click -= new RoutedEventHandler(BtnNext_Click);
                _btnNext.Click += new RoutedEventHandler(BtnNext_Click);

                Binding allowNextBinding = new Binding("AllowNext");
                allowNextBinding.Source = this;
                _btnNext.SetBinding(Button.IsEnabledProperty, allowNextBinding);
            }

            _btnPrevious = this.FindName("BtnPrevious") as Button;
            if (_btnPrevious != null)
            {
                _btnPrevious.Click -= new RoutedEventHandler(BtnPrevious_Click);
                _btnPrevious.Click += new RoutedEventHandler(BtnPrevious_Click);
            }
        }

        #endregion

        #region Eventos dos botões de Próximo de Anterior

        void BtnPrevious_Click(object sender, RoutedEventArgs e)
        {
            OnPreviousClick(e);
            if (!e.Handled)
            {
                this.NavigationAction = StepNavigationAction.Previous;
                this.Close();
            }
            else
                this.NavigationAction = null;
        }

        protected virtual void OnPreviousClick(RoutedEventArgs e)
        {

        }

        void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            OnNextClick(e);
            if (!e.Handled)
            {
                this.NavigationAction = StepNavigationAction.Next;
                this.Close();
            }
            else
                this.NavigationAction = null;
        }

        protected virtual void OnNextClick(RoutedEventArgs e)
        {

        }

        protected override void ChangeStyle()
        {
            this.SetResourceReference(BaseWizardWindow.StyleProperty, "BaseWizardWindowStyle");
        }

        #endregion
    }
}
